package jsf;

import bean.RptPensionesMaternal;
import jsf.util.JsfUtil;
import jsf.util.PaginationHelper;
import ejb.RptPensionesMaternalFacade;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

@ManagedBean(name = "rptPensionesMaternalController")
@SessionScoped
public class RptPensionesMaternalController implements Serializable {

    private RptPensionesMaternal current;
    private DataModel items = null;
    private List<RptPensionesMaternal> lsRpt = null;
    
    @EJB
    private ejb.RptPensionesMaternalFacade ejbFacade;
    private PaginationHelper pagination;
    private int selectedItemIndex;

    public RptPensionesMaternalController() {
    }

    public RptPensionesMaternal getSelected() {
        if (current == null) {
            current = new RptPensionesMaternal();
            selectedItemIndex = -1;
        }
        return current;
    }

    private RptPensionesMaternalFacade getFacade() {
        return ejbFacade;
    }

    public PaginationHelper getPagination() {
        if (pagination == null) {
            pagination = new PaginationHelper(10) {

                @Override
                public int getItemsCount() {
                    return getFacade().count();
                }

                @Override
                public DataModel createPageDataModel() {
                    return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
                }
            };
        }
        return pagination;
    }

    public String prepareList() {
        //recreateModel();
        llenarLista();
        return "RptPensionesMaternalList";
    }
    
    public void llenarLista(){
        lsRpt = new ArrayList<RptPensionesMaternal>();
        lsRpt.addAll(ejbFacade.findAll());        
    }

    public String prepareView() {
        //current = (RptPensionesMaternal) getItems().getRowData();
        //selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
        current = new RptPensionesMaternal();
        FacesContext context = FacesContext.getCurrentInstance();
        Map map = context.getExternalContext().getRequestParameterMap();
        String param = (String) map.get("rptItem");        
        current = (RptPensionesMaternal) ejbFacade.findById(param);
        return "RptPensionesMaternalView";
    }
   
    public DataModel getItems() {
        if (items == null) {
            items = getPagination().createPageDataModel();
        }
        return items;
    }

    private void recreateModel() {
        items = null;
    }

    private void recreatePagination() {
        pagination = null;
    }

    public SelectItem[] getItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
    }

    public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
    }

    /**
     * @return the lsRpt
     */
    public List<RptPensionesMaternal> getLsRpt() {
        return lsRpt;
    }

    /**
     * @param lsRpt the lsRpt to set
     */
    public void setLsRpt(List<RptPensionesMaternal> lsRpt) {
        this.lsRpt = lsRpt;
    }

    @FacesConverter(forClass = RptPensionesMaternal.class)
    public static class RptPensionesMaternalControllerConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            RptPensionesMaternalController controller = (RptPensionesMaternalController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "rptPensionesMaternalController");
            return controller.ejbFacade.find(getKey(value));
        }

        java.lang.String getKey(String value) {
            java.lang.String key;
            key = value;
            return key;
        }

        String getStringKey(java.lang.String value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof RptPensionesMaternal) {
                RptPensionesMaternal o = (RptPensionesMaternal) object;
                return getStringKey(o.getCodLiquidacion());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + RptPensionesMaternalController.class.getName());
            }
        }
    }
}
